package com.xt.website_manager.service.impl;
/*
 * @Author 爱尚实训赵国鑫
 * @ClassName  CategoryServiceImpl
 * @Date 2019/8/18/10:10
 * @Description TODO:(一句话描述这个类)
 */

import com.xt.website_manager.dao.CategoryDao;
import com.xt.website_manager.po.Category;
import com.xt.website_manager.po.CategoryExt;
import com.xt.website_manager.po.CategorySecond;
import com.xt.website_manager.service.CategoryService;
import com.xt.website_manager.util.FileUploadUtil;
import com.xt.website_manager.util.PublicUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Transactional
@Service
public class CategoryServiceImpl implements CategoryService {

    @Resource
    private CategoryDao categoryDao;

    /**
     * 获取一二级类目集合
     * @return List<CategoryExt>
     */
    @Override
    public ArrayList<CategoryExt<CategorySecond>> getAllCategories() {
        return categoryDao.getAllCategories();
    }

    /**
     * 获取一级类目集合
     * @return
     */
    @Override
    public List<Category> getCategories() {
        return categoryDao.getCategories();
    }

    /**
     * 根据一级类目id获取二级类目集合
     * @param cid
     * @return
     */
    @Override
    public List<CategorySecond> findCategorySecondByCid(Integer cid) {
        return categoryDao.getCategorySecondsByCid(cid);
    }

    /**
     * 根据一级类目查询二级类目总数
     * @param id
     * @return
     */
    @Override
    public Integer findCategoryCountByCid(Integer id) {
        return categoryDao.findCategoryCountByCid(id);
    }

    @Override
    public List<CategorySecond> getAllCategorysecond() {
        return categoryDao.getAllCategorysecond();
    }

    /**
     * 根据类目类型查询二级类目
     */
    @Override
    public List<CategorySecond> findCategorySecondByType(Integer type) {
        return categoryDao.findCategorySecondByType(type);
    }

    /**
     * 根据二级类目查询其对应的一级类目
     */
    @Override
    public Category findCategoryByCsid(Integer csid) {
        return categoryDao.findCategoryByCsid(csid);
    }

    /**
     *根据一级类目id查询一级类目
     */
    @Override
    public Category getCategoryByCid(Integer cid) {
        return categoryDao.getCategoryByCid(cid);
    }


    /**
     * 根据二级类目id查询二级类目
     * @param id
     * @return
     */
    @Override
    public CategorySecond getCategorySecond(Integer id) {
        return categoryDao.findCategorySecondByCsid(id);
    }



    /**
     * 修改一级类目
     * @param category 一级类目对象
     * @param file     banner图file
     * @param prevPath 原文件路径
     * @param realPath 项目路径
     */

    @Override
    public void updateCategory(Category category, MultipartFile file, String prevPath, String realPath) {
        String image ="";
        if(file.getOriginalFilename()!=null&&!"".equals(file.getOriginalFilename().trim())) {
            //添加类目banner图片
            try {
                File upload_dir = new File(realPath);
                File directoryFile = FileUploadUtil.makeFullDirectoryObject(upload_dir);
                // 把文件上传至path的路径，文件名改为为当前时间+原始文件名.扩展名
                File localFile = new File(directoryFile, UUID.randomUUID() + file.getOriginalFilename());
                //执行上传
                file.transferTo(localFile);
                category.setImage("/images/" + directoryFile.getName() + "/" + localFile.getName());
                categoryDao.updateCategory(category);
            }catch (Exception e){
                PublicUtil.delPic(realPath+image);
                throw new RuntimeException("修改文件出错");
            }
            if(PublicUtil.isEmpty(prevPath)){
                //删除原文件
                PublicUtil.delPic(realPath + prevPath);
            }
        }else{
            category.setImage(prevPath);
            categoryDao.updateCategory(category);
        }

    }

    /**
     * 添加类目
     * @param category
     * @param file
     * @param realPath
     */
    @Override
    public void addCategory(Category category, MultipartFile file, String realPath) {
        String image ="";
        if(!file.isEmpty()) {
            //添加类目banner图片
            try {
                File upload_dir = new File(realPath);
                File directoryFile = FileUploadUtil.makeFullDirectoryObject(upload_dir);
                // 把文件上传至path的路径，文件名改为为当前时间+原始文件名.扩展名
                File localFile = new File(directoryFile, UUID.randomUUID() + file.getOriginalFilename());
                //执行上传
                file.transferTo(localFile);
                category.setImage("/images/" + directoryFile.getName() + "/" + localFile.getName());
                categoryDao.addCategory(category);
            }catch (Exception e){
                PublicUtil.delPic(realPath+image);
                throw new RuntimeException("添加类目出错");
            }
        }else{
            category.setImage(image);
            categoryDao.addCategory(category);
        }
    }


    /**
     * 根据id删除一级类目
     * @param id
     * @param realPath
     */
    @Override
    public void delCategory(Integer id, String realPath) {
        Category category = categoryDao.getCategoryByCid(id);
        //删除
        categoryDao.delCategory(id);
        //删除banner图片
        if(category.getImage()!=null&&!"".equals(category.getImage().trim())){
            PublicUtil.delPic(realPath+category.getImage());
        }

    }

    /**
     * 更改一级类目优先级
     * @param category
     */
    @Override
    public void changeCategorySort(Category category) {
        categoryDao.changeCategorySort(category);
    }

    /**
     * 查询一级类目详情，并查询该类目下的全部二级类目
     */
    @Override
    public CategoryExt<CategorySecond> findCategoryByCid(Integer cid) {
        return categoryDao.findCategoryByCid(cid);
    }

    /**
     * 添加二级类目
     * @param categorySecond
     * @param file
     * @param realPath
     */
    @Override
    public void addCategorySecond(CategorySecond categorySecond, MultipartFile file, String realPath) {
        String image ="";
        if(file.getOriginalFilename()!=null&&!"".equals(file.getOriginalFilename().trim())) {
            //添加类目banner图片
            try {
                File upload_dir = new File(realPath);
                File directoryFile = FileUploadUtil.makeFullDirectoryObject(upload_dir);
                // 把文件上传至path的路径，文件名改为为当前时间+原始文件名.扩展名
                File localFile = new File(directoryFile, UUID.randomUUID() + file.getOriginalFilename());
                //执行上传
                file.transferTo(localFile);
                categorySecond.setImage("/images/" + directoryFile.getName() + "/" + localFile.getName());
                categoryDao.addCategorySecond(categorySecond);
            }catch (Exception e){
                PublicUtil.delPic(realPath+image);
                throw new RuntimeException("添加类目出错");
            }
        }else{
            categoryDao.addCategorySecond(categorySecond);
        }
    }


    /**
     * 删除二级类目
     */
    @Override
    public void delCategorySecond(Integer csid, String realPath) {
        CategorySecond categorySecond = categoryDao.findCategorySecondByCsid(csid);
        //删除
        categoryDao.delCategorySecond(csid);
        //删除banner图片
        if(categorySecond.getImage()!=null&&!"".equals(categorySecond.getImage().trim())){
            PublicUtil.delPic(realPath+categorySecond.getImage());
        }
    }


    /**
     * 更改二级类目优先级
     */
    @Override
    public void changeCategoryCsSort(CategorySecond categorySecond) {
        categoryDao.changeCategoryCsSort(categorySecond);
    }


    /**
     * 修改二级类目
     * @param categorySecond
     * @param file
     * @param prevPath
     * @param realPath
     */
    @Override
    public void updateCategorySecond(CategorySecond categorySecond, MultipartFile file, String prevPath, String realPath) {
        String image ="";
        if(file.getOriginalFilename()!=null&&!"".equals(file.getOriginalFilename().trim())) {
            //添加类目banner图片
            try {
                File upload_dir = new File(realPath);
                File directoryFile = FileUploadUtil.makeFullDirectoryObject(upload_dir);
                // 把文件上传至path的路径，文件名改为为当前时间+原始文件名.扩展名
                File localFile = new File(directoryFile, UUID.randomUUID() + file.getOriginalFilename());
                //执行上传
                file.transferTo(localFile);
                categorySecond.setImage("/images/" + directoryFile.getName() + "/" + localFile.getName());
                categoryDao.updateCategorySecond(categorySecond);
            }catch (Exception e){
                PublicUtil.delPic(realPath+image);
                throw new RuntimeException("修改文件出错");
            }
            if(PublicUtil.isEmpty(prevPath)){
                //删除原文件
                PublicUtil.delPic(realPath + prevPath);
            }
        }else{
            categorySecond.setImage(prevPath);
            categoryDao.updateCategorySecond(categorySecond);
        }
    }

    /**
     * 更改所有一级类目下的二级类目类型
     * @param category
     */
    @Override
    public void updateCategorySecondType(Category category) {
        //categoryDao.updateCategorySecondType(category);
    }
}
